<template>
    <!--我的购物车-->
    <div class="contain">
        <div class="product-header">
            <div class="title">请确认订单</div>
        </div>
        <div class="main-box">
            <div class="address-box">
                <div class="title">
                    <div class="item">
                        收货人信息
                    </div>
                </div>
                <div class="address-content">
                    <ul class="address-box">
                        <li class="address" v-for="address in addressList" :key="address.id"
                        @click="handleChangeAddress(address)">
                            <span class="default-tag" v-if="address.is_default">默认地址</span>
                            <div class="form">
                                <span class="label">姓名</span>
                                <span class="value">{{address.consignee}}</span>
                            </div>
                            <div class="form">
                                <span class="label">联系方式</span>
                                <span class="value">{{address.mobile}}</span>
                            </div>
                            <div class="form">
                                <span class="label">收货地址</span>
                                <span class="value">{{address.province}} {{address.city}} {{address.xianqu}}</span>
                            </div>
                            <div class="icon-select" v-show="addressChoose.id===address.id"></div>
                        </li>
                        <li class="add-box" @click="handleAddAddress">
                            <span>+ 新增地址</span>
                        </li>
                    </ul>
                </div>
            </div>
            <ul class="tb-box">
                <li class="tbl title">
                    <div class="item product">
                        商品
                    </div>
                    <div class="item price">
                        单价
                    </div>
                    <div class="item number">
                        数量
                    </div>
                    <div class="item count">
                        小计
                    </div>
                </li>
                <li class="tbl goods" v-for="item in list" :key="item.cid">
                    <div class="item product">
                        <img class="faceImg" :src="item.dc.fengmian_pic_url" alt="">
                        <span class="name">{{item.dc.name}}</span>
                    </div>
                    <div class="item price">
                        <span>{{item.dc.one_price| fixNumber}}</span>
                    </div>
                    <div class="item number">
                        {{item.num}}
                    </div>
                    <div class="item count">
                        <span>{{item.price| fixNumber}}</span>
                    </div>
                </li>
                <li class="nodata" v-if="list.length === 0">
                    <p>暂无数据</p>
                </li>
                <li class="footer"></li>
            </ul>
            <div class="count-box">
                <p>共计{{totalNum}}件商品，总计{{price}}元，应付<span>￥{{price}}</span></p>
                <div class="button" @click="jumpOrder">立即下单</div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'QuestionBank',
    components: {
        /**/
    },
    data() {
        return {
            addressList: [],
            list: [],
            price: 0,
            totalNum: 0,
            addressChoose: null
        };
    },
    computed: {

    },
    watch: {
    },
    mounted() {
        if(this.$util.authLogin()) {
            this.createOrder(this.$route.query.gid)
        }
    },
    methods: {
        // 查询信息
        createOrder(gid) {
            if(!gid) {
                this.$util.alert('获取订单信息失败');
                return
            }
            this.$api.ajaxData({
                pathName: 'initFky',
                authorization: true,
                params: {
                    uid: this.$api.getAccount().id,
                    gid: gid
                }
            })
                .then(res => {
                    let temp = []
                    let totalPrice = 0;
                    this.totalNum = 0;
                    res.obj.goodses.forEach(item => {
                        if(item.dc) {
                            item.num = item.num || 1;
                            let price = item.dc.one_price * item.num;
                            this.totalNum += item.num
                            totalPrice = totalPrice + price;
                            temp.push(Object.assign(item,{
                                price: price
                            }))
                        }
                    })
                    this.price = totalPrice;
                    this.list = temp;
                    this.addressList = res.obj.addresses;
                    if(this.addressList.length >0) {
                        this.addressChoose = this.addressList[0]
                    }
                })
                .catch(() => {

                });
        },
        // 创建订单
        jumpOrder() {
//            let that = this;
            if(!this.list.length === 0) {
                this.$util.alert('订单内容为空');
                return
            }
            if(!this.addressChoose.id) {
                this.$util.alert('请先选择地址');
                return
            }
            let goods = {}
            this.list.forEach(item=> {
                goods[item.dc.id] = item.num
            })
            this.$api.ajaxData({
                pathName: 'saveOrder',
                authorization: true,
                method: 'get',
                params: {
                    goodMap: goods,
                    order_from: 1,
                    addressid: this.addressChoose.id
                }
            })
                .then(res => {
                    this.$router.push({name: 'order',query: {
                        orderId: res.obj.id,
                        price: (this.price*100).toFixed(0),
                        desc: this.list[0].dc.name + '等共计'+this.totalNum+'件商品'
                    }})
                })
                .catch(() => {

                });
        },
        // 添加地址
        handleAddAddress() {
            this.$router.push({name: 'setting', query: {
                page: 'addressMsg',
                title: '地址管理'
            }})
        },
        // 更换地址
        handleChangeAddress(item) {
            this.addressChoose = item
        }
    }
};
</script>
<style scoped lang="scss">
    .contain {
        .product-header {
            padding: 40px 0;
            height: 110px;
            color: #fff;
            &:before {
                content: '';
                position: absolute;
                top: 0;
                left: 0;
                width: 100vw;
                height: 330px;
                background: url('../../assets/image/back/u542.jpg') no-repeat;
                background-size: 100% 100%;
            }
            .title {
                position: relative;
                line-height: 30px;
                padding: 0 30px;
                font-size: 28px;
            }

        }
        .main-box {
            position: relative;
            background: #fff;
            border-radius: 6px;
            box-shadow: 0 0 5px rgba(0,0,0,0.05);
            .address-box {
                padding: 0 30px;
                font-size: 14px;
                .title {
                    padding: 10px 0;
                    line-height: 30px;
                    font-weight: bold;
                    color: #333;
                    border-bottom: 1px solid #f7f7f7;
                }
                .address-content {
                    .address-box {
                        padding: 20px 0;
                        li {
                            position: relative;
                            float: left;
                            width: calc(33.33% - 20px);
                            padding: 15px 10px;
                            margin-bottom: 20px;
                            margin-right: 20px;
                            font-size: 13px;
                            color: #333333;
                            border: 1px solid #e4e4e4;
                            box-sizing: border-box;
                            &.address {
                                .default-tag {
                                    position: absolute;
                                    top: 3px;
                                    right: 0;
                                    height: 30px;
                                    line-height: 30px;
                                    padding: 0 20px;
                                    border-radius: 2px;
                                    font-size: 12px;
                                    color: #FFFFFF;
                                    background: #ff8080;
                                }
                                .form {
                                    display: flex;
                                    align-items: flex-start;
                                    margin-bottom: 10px;
                                    &:nth-child(3) {
                                        height: 40px;
                                    }
                                    .label {
                                        flex-shrink: 0;
                                        width: 70px;
                                    }
                                    .value {
                                        @include ellipsis-line;
                                    }
                                }
                                .option {
                                    .option-btn {
                                        float: left;
                                        width: 70px;
                                        height: 30px;
                                        line-height: 30px;
                                        margin-right: 10px;
                                        color: #999;
                                        border: 1px solid #e4e4e4;
                                        border-radius: 3px;
                                        text-align: center;
                                        cursor: pointer;
                                    }
                                    &:after {
                                        content: '';
                                        display: block;
                                        clear: both;
                                    }
                                }
                                &:before {
                                    content: '';
                                    position: absolute;
                                    top: 0;
                                    left: 0;
                                    display: block;
                                    width: 100%;
                                    height: 3px;
                                    background: linear-gradient(to right, #ff8080 0,#ff8080 30px,#fff 30px,#fff 40px,#6699ff 40px, #6699ff 70px,#fff 70px,#fff 80px) repeat-x;
                                    background-size: 80px 100%;
                                }
                                &:after {
                                    content: '';
                                    position: absolute;
                                    bottom: 0;
                                    left: 0;
                                    display: block;
                                    width: 100%;
                                    height: 3px;
                                    background: linear-gradient(to right, #ff8080 0,#ff8080 30px,#fff 30px,#fff 40px,#6699ff 40px, #6699ff 70px,#fff 70px,#fff 80px) repeat-x;
                                    background-size: 80px 100%;
                                }
                            }
                            &.add-box {
                                display: flex;
                                height: 140px;
                                align-items: center;
                                justify-content: center;
                                border: 1px dashed #e4e4e4;
                                text-align: center;
                                cursor: pointer;
                                span {
                                    font-size: 20px;
                                    color: #666666;
                                    line-height: 28px;
                                }
                            }
                            &:nth-child(3n) {
                                margin-right: 0;
                            }
                            .icon-select {
                                position: absolute;
                                right: 0;
                                bottom: 2px;
                                display: block;
                                width: 40px;
                                height: 40px;
                                background: url("../../assets/image/icon/u2949.png") no-repeat;
                                background-size: contain;
                            }
                        }

                    }&:after {
                         content: '';
                         display: block;
                         clear: both;
                     }
                }
            }
            .tb-box {
                position: relative;
                margin-bottom: 20px;
                font-size: 14px;
                color: rgb(51,51,51);
                .tbl {
                    display: flex;
                    padding: 15px 0;
                    align-items: center;
                    .item {
                        padding-left: 25px;
                        flex: 2;

                    }
                    .checkBox {
                        flex: 1;
                    }
                    .product {
                        line-height: 20px;
                        flex: 6;
                        .faceImg {
                            float: left;
                            width: 130px;
                            height: 70px;
                            margin-right: 15px;
                            border-radius: 5px;
                            background: #f7f7f7;
                        }
                        .name {
                            text-align: left;
                            clear: both;
                        }
                    }
                    .option {
                        a {
                            margin-right: 10px;
                        }
                    }
                }
                .title {
                    height: 78px;
                    border-top-left-radius: 6px;
                    border-top-right-radius: 6px;
                    background: #f8f8f8;
                    font-size: 12px;
                    .product {
                        text-align: center;
                    }
                }
                .nodata {
                    padding: 45px;
                    text-align: center;
                    color: #999;
                }
                .footer {
                    height: 78px;
                    background: #f8f8f8;
                }
            }
            .count-box {
                display: flex;
                padding: 0 30px;
                align-items: center;
                justify-content: space-between;
                border-top: 1px solid #f7f7f7;
                height: 80px;
                font-size: 14px;
                color: #666;
                p {
                    span {
                        font-size: 20px;
                        font-weight: bold;
                        color: #FF6666;
                    }
                }
                .button {
                    height: 40px;
                    width: 126px;
                    line-height: 40px;
                    border-radius: 20px;
                    background: #FF6666;
                    text-align: center;
                    color: #fff;
                    cursor: pointer;
                }
                .pay-box {
                   display: flex;
                    .pay {
                        margin-right: 20px;
                        height: 40px;
                        width: 126px;
                        line-height: 40px;
                        border-radius: 20px;
                        background: #FF6666;
                        text-align: center;
                        color: #fff;
                        cursor: pointer;
                    }
                }
            }
        }
    }
</style>
